package logic

import (
	"context"
	"project/pkg"
	"project/projectrpc/projectrpc"

	"project/projectapi/internal/svc"
	"project/projectapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddLogic {
	return &UserAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserAddLogic) UserAdd(req *types.UserAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	if !pkg.Tel(req.Tel) {
		return &types.Response{
			Message: "手机号格式错误",
			Code:    500,
			Data:    nil,
		}, nil
	}

	add, err := l.svcCtx.ProjectRpc.UserAdd(l.ctx, &projectrpc.UserAddRequest{
		Username: req.Username,
		Tel:      req.Tel,
		Email:    req.Email,
		Price:    float32(req.Price),
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "添加成功",
		Code:    200,
		Data:    add,
	}, nil
}
